package com.randb.easyhttp.subscriber;

import android.content.Context;

import com.randb.easyhttp.callback.Callback;
import com.randb.easyhttp.callback.ProgressDialogCallback;
import com.randb.easyhttp.exception.ApiException;

/**
 * User: RandBII
 * Date: 2020/5/11
 * Description:
 */
public class CallbackSubscriber<T> extends BaseSubscriber<T> {

    public Callback<T> callback;

    public CallbackSubscriber(Context context, Callback<T> callback) {
        super(context);
        this.callback = callback;
        if (callback instanceof ProgressDialogCallback) {
            ((ProgressDialogCallback) callback).subscription(this);
        }
    }

    @Override
    protected void onStart() {
        super.onStart();
        if (callback != null) {
            callback.onStart();
        }
    }

    @Override
    public void onError(ApiException e) {
        if (callback != null) {
            callback.onError(e);
        }

    }

    @Override
    public void onComplete() {
        if (callback != null) {
            callback.onFinish();
        }
    }

    @Override
    public void onNext(T t) {
        super.onNext(t);
        if (callback != null) {
            callback.onSuccess(t);
        }
    }
}
